_package("compass.page");

_import("caf.ui.Page");

_class("HomePage", Page, function(){

	var currentAngle = 0,
	angleEl,
	compassEl;

	if(!CloudAPI.Device.getCurrentHeading){
		CloudAPI.Device.getCurrentHeading = function(){
			if(!this.angle){
				this.angle = 0;
			}
			return this.angle++;
		}
	}
	this._init = function(){
		_super._init.call(this);
	};
	this.create = function(parent){
		var obj = this.createTplElement(parent, "home.xml");
		this.init(obj);
		this.afterReady();
		return obj;
	};
	this.afterReady = function(){
		angleEl = document.querySelector("#angle");
		compassEl = document.getElementById("compass");
		this.startLoop();
	};
	this.startLoop = function(){
		var _this = this;
		setTimeout(function(){
			_this.startLoop();
		}, 100);
		_this.update();
	};
	this.update = function(){
		var angle = Math.round(CloudAPI.Device.getCurrentHeading()) % 360;
		angleEl.innerHTML = this.getHeadingByAngle(angle);
		compassEl.style["-webkit-transform"] = "rotate("+ (-angle)+"deg)";
	};
	this.getHeadingByAngle = function(angle){
		var s;
		if(angle < 22.5 || angle >= 337.5){
			s = "北";
		}else if(angle >= 22.5 && angle < 67.5){
			s = "东北";
		}else if(angle >= 67.5 && angle < 112.5){
			s = "东";
		}else if(angle >= 112.5 && angle < 157.5){
			s = "东南";
		}else if(angle >= 157.5 && angle < 202.5){
			s = "南";
		}else if(angle >= 202.5 && angle < 247.5){
			s = "西南";
		}else if(angle >= 247.5 && angle < 292.5){
			s = "西";
		}else if(angle >= 292.5 && angle < 337.5){
			s = "西北";
		}
		s += angle+"°";
		return s;
	}
	this.init = function(){
		_super.init.apply(this, arguments);
		this.initComponents();
		this.initActionElements();
	};
	this.dispose = function(){
		_super.dispose.apply(this);
	};
});